package org.game.msgBuilder;


import com.xiaoleilu.hutool.date.DateUtil;
import org.cfg.MonthCardCfg;
import org.cfg.RechargeCfg;
import org.cfg.RechargeGiftBagCfg;
import org.cfg.cache.MonthCardCfgCache;
import org.cfg.cache.RechargeGiftBagCfgCache;
import org.constant.RechargeConstant;
import org.game.db.bean.MonthCard;
import org.game.db.bean.RechargeGiftBagRecord;
import org.game.protobuf.s2c.S2CItemMsg;
import org.game.protobuf.s2c.S2CRechargeMsg;
import org.game.protobuf.s2c.S2CShareMsg;
import org.utils.TimeUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by fxf on 2018-2-5.
 */
public class RechargeMsgBuilder {

    public static S2CRechargeMsg.GetOrderNo buildGetOrderNoMsg(String orderNo) {
        S2CRechargeMsg.GetOrderNo.Builder builder = S2CRechargeMsg.GetOrderNo.newBuilder();
        builder.setOrderNo(orderNo);
        return builder.build();
    }

    public static S2CRechargeMsg.RechargeSuccess buildRechargeSuccessMsg(int cid) {
        S2CRechargeMsg.RechargeSuccess.Builder builder = S2CRechargeMsg.RechargeSuccess.newBuilder();
        builder.setCid(cid);
        return builder.build();
    }

    public static S2CRechargeMsg.GetMonthCardInfo buildGetMonthCardInfoMsg(MonthCard mc, S2CShareMsg.ChangeType changeType) {
        S2CRechargeMsg.GetMonthCardInfo.Builder builder = S2CRechargeMsg.GetMonthCardInfo.newBuilder();
        builder.setCt(changeType);
        if (mc == null) {
            builder.setCardCid(0)
                    .setLastGainDate(0)
                    .setSurplusGainCount(0);
        } else {
            builder.setCardCid(mc.getCid())
                    .setLastGainDate((mc.getLastGainDate() == null ? 0 : (int) (mc.getLastGainDate().getTime() / 1000)))
                    .setSurplusGainCount((int) DateUtil.betweenDay(new Date(), mc.getGainLimitDate() == null ? new Date() : mc.getGainLimitDate(), true));
        }
        return builder.build();
    }


    public static S2CRechargeMsg.BuyRecordInfo buildBuyRecordInfo(RechargeGiftBagRecord record, S2CShareMsg.ChangeType ct) {
        S2CRechargeMsg.BuyRecordInfo.Builder builder = S2CRechargeMsg.BuyRecordInfo.newBuilder();
        builder.setBuyCount(record.getBuyCount())
                .setCid(record.getCid())
                .setCt(ct);
        return builder.build();
    }

    public static S2CRechargeMsg.GetBuyRecordInfo buildGetBuyRecordInfoMsg(List<RechargeGiftBagRecord> list, S2CShareMsg.ChangeType ct) {
        S2CRechargeMsg.GetBuyRecordInfo.Builder builder = S2CRechargeMsg.GetBuyRecordInfo.newBuilder();
        list.forEach(record -> builder.addInfo(buildBuyRecordInfo(record, ct)));
        return builder.build();
    }

    public static S2CRechargeMsg.BuyMonthCardInfo buildBuyMonthCardInfoMsg(MonthCard mc, S2CItemMsg.ItemList itemList, S2CShareMsg.ChangeType ct) {
        S2CRechargeMsg.BuyMonthCardInfo.Builder builder = S2CRechargeMsg.BuyMonthCardInfo.newBuilder();
        builder.setMonthCardInfo(buildGetMonthCardInfoMsg(mc, ct));
        if (itemList != null) {
            builder.setItemList(itemList);
        }
        return builder.build();
    }


    public static S2CRechargeMsg.GetRechargeCfg buildGetRechargeCfgMsg(List<RechargeCfg> cfgList) {
        S2CRechargeMsg.GetRechargeCfg.Builder builder = S2CRechargeMsg.GetRechargeCfg.newBuilder();
        cfgList.forEach(cfg -> {
            S2CRechargeMsg.RechargeCfg.Builder rechargeCfgBuilder = S2CRechargeMsg.RechargeCfg.newBuilder();
            rechargeCfgBuilder.setId(cfg.getId())
                    .setPrice(cfg.getPrice());
            if (cfg.getType() == RechargeConstant.RECHARGE_GOODS_TYPE_MONTH_CARD) {
                MonthCardCfg monthCardCfg = MonthCardCfgCache.me().getById(cfg.getGoodsId());
                S2CRechargeMsg.MonthCardCfg.Builder monthCardCfgBuilder = S2CRechargeMsg.MonthCardCfg.newBuilder();
                monthCardCfgBuilder.setUpgradeId(monthCardCfg.getUpgradeId())
                        .setDays(monthCardCfg.getDays())
                        .setType(monthCardCfg.getType())
                        .setIcon(monthCardCfg.getIcon())
                        .setName(monthCardCfg.getName())
                        .setDes1(monthCardCfg.getDes1());
                for (Object o : monthCardCfg.getGainItem().entrySet()) {
                    Map.Entry<Integer, Integer> e = (Map.Entry<Integer, Integer>) o;
                    S2CShareMsg.RewardsMsg.Builder gainItemBuilder = S2CShareMsg.RewardsMsg.newBuilder();
                    gainItemBuilder.setId(e.getKey())
                            .setNum(e.getValue());
                    monthCardCfgBuilder.addGainItem(gainItemBuilder);
                }
                for (Object o : monthCardCfg.getPresentItem().entrySet()) {
                    Map.Entry<Integer, Integer> e = (Map.Entry<Integer, Integer>) o;
                    S2CShareMsg.RewardsMsg.Builder presentItemBuilder = S2CShareMsg.RewardsMsg.newBuilder();
                    presentItemBuilder.setId(e.getKey())
                            .setNum(e.getValue());
                    monthCardCfgBuilder.addPresentItem(presentItemBuilder);
                }
                monthCardCfgBuilder.setRechargeCfg(rechargeCfgBuilder);
                builder.addMonthCardCfg(monthCardCfgBuilder);
            } else {
                RechargeGiftBagCfg rechargeGiftBagCfg = RechargeGiftBagCfgCache.me().getById(cfg.getGoodsId());
                S2CRechargeMsg.RechargeGiftBagCfg.Builder rechargeGiftBagCfgBuilder = S2CRechargeMsg.RechargeGiftBagCfg.newBuilder();
                rechargeGiftBagCfgBuilder.setRechargeCfg(rechargeCfgBuilder)
                        .setBuyCount(rechargeGiftBagCfg.getBuyCount())
                        .setResetType(rechargeGiftBagCfg.getResetType())
                        .setResetDate(rechargeGiftBagCfg.getResetDate())
                        .setType(rechargeGiftBagCfg.getType())
                        .setName(rechargeGiftBagCfg.getName())
                        .setIcon(rechargeGiftBagCfg.getIcon())
                        .setTag(rechargeGiftBagCfg.getTag())
                        .setTagDes(rechargeGiftBagCfg.getTagDes())
                        .setTagDes2(rechargeGiftBagCfg.getTagDes2())
                        .setDes1(rechargeGiftBagCfg.getDes1())
                        .setDes2(rechargeGiftBagCfg.getDes2())
                        .setOrder(rechargeGiftBagCfg.getOrder());

                for (Object o : rechargeGiftBagCfg.getItem().entrySet()) {
                    Map.Entry<Integer, Integer> e = (Map.Entry<Integer, Integer>) o;
                    S2CShareMsg.RewardsMsg.Builder itemBuilder = S2CShareMsg.RewardsMsg.newBuilder();
                    itemBuilder.setId(e.getKey())
                            .setNum(e.getValue());
                    rechargeGiftBagCfgBuilder.addItem(itemBuilder);
                }

                for (Object o : rechargeGiftBagCfg.getFirstBuyItem().entrySet()) {
                    Map.Entry<Integer, Integer> e = (Map.Entry<Integer, Integer>) o;
                    S2CShareMsg.RewardsMsg.Builder itemBuilder = S2CShareMsg.RewardsMsg.newBuilder();
                    itemBuilder.setId(e.getKey())
                            .setNum(e.getValue());
                    rechargeGiftBagCfgBuilder.addFirstBuyItem(itemBuilder);
                }

                if (rechargeGiftBagCfg.getStartDate() != null && rechargeGiftBagCfg.getStartDate().length != 0) {
                    rechargeGiftBagCfgBuilder.setStartDate((int) (TimeUtil.getAssignCalendar(rechargeGiftBagCfg.getStartDate()).getTimeInMillis() / 1000));
                }
                if (rechargeGiftBagCfg.getEndDate() != null && rechargeGiftBagCfg.getEndDate().length != 0) {
                    rechargeGiftBagCfgBuilder.setEndDate((int) (TimeUtil.getAssignCalendar(rechargeGiftBagCfg.getEndDate()).getTimeInMillis() / 1000));
                }
                if (rechargeGiftBagCfg.getPlayerLevel() != null && rechargeGiftBagCfg.getPlayerLevel().length > 0) {
                    for (int level : rechargeGiftBagCfg.getPlayerLevel()) {
                        rechargeGiftBagCfgBuilder.addPlayerLevel(level);
                    }
                }
                builder.addRechargeGiftBagCfg(rechargeGiftBagCfgBuilder);
            }
        });
        return builder.build();
    }

}
